<template>
  <el-dialog
    :title="title"
    :visible.sync="openSheet"
    width="800px"
    :destroy-on-close="true"
    :close-on-click-modal="false"
    :before-close="cancel"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('samplesItem')" prop="item_no" :error="error.item_no">
            <el-input
              v-model="form.item_no"
              maxlength="15"
              :disabled="!createFlag"
              show-word-limit
              :placeholder="$t('inputSamplesItem')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('samplesMenuCode')" prop="menu" :error="error.menu">
            <el-select
              v-model="form.menu"
              style="width: 100%"
            >
              <el-option v-for="item in menuOptions" :key="item.id" :label="item.code" :value="item.id">
                <span style="float: left">{{ item.code }}</span>
                <span style="float: right; font-size: 13px; color: var(--el-text-color-secondary)">{{ item.name }}</span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('samplesWarehouse')" prop="warehouse" :error="error.warehouse">
            <el-input
              v-model="form.warehouse"
              maxlength="15"
              show-word-limit
              :placeholder="$t('inputSamplesWarehouse')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('samplesLocation')" prop="location" :error="error.location">
            <el-input
              v-model="form.location"
              maxlength="15"
              show-word-limit
              :placeholder="$t('inputSamplesLocation')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item :label="$t('samplesValidDate')" prop="valid_date" :error="error.valid_date">
            <el-date-picker
              v-model="form.valid_date"
              :placeholder="$t('inputSamplesValidDate')"
              type="date"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item :label="$t('samplesInvalidDate')" prop="invalid_date" :error="error.invalid_date">
            <el-date-picker
              v-model="form.invalid_date"
              :placeholder="$t('inputSamplesInvalidDate')"
              type="date"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item :label="$t('samplesQuantity')" prop="quantity" :error="error.quantity">
            <el-input
              v-model="form.quantity"
              type="number"
              :placeholder="$t('inputSamplesQuantity')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('samplesNote')" prop="note" :error="error.note">
            <el-input
              v-model="form.note"
              type="textarea"
              :rows="1"
              autosize
              :placeholder="$t('inputSamplesNote')"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button v-if="createFlag" type="primary" :disabled="!isModified" @click="handleCreate">{{
        $t('create')
      }}</el-button>
      <el-button v-else type="primary" :disabled="!isModified" @click="handleUpdate">{{
        $t('update')
      }}</el-button>
      <el-button @click="cancel">{{ $t('cancel') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
import sheetMixin from '@/mixins/SheetMixin'
import {
  createSamplesApi,
  updateSamplesApi
} from '../api/samples.api';
import { getDropDownMenuListApi } from '@/api/common/common'
export default {
  mixins: [sheetMixin],
  data() {
    return {
      formRef: 'form',
      createTitle: this.$t('samplesCreate'),
      updateTitle: this.$t('samplesUpdate'),
      menuOptions: [],
      actionApi: {
        'create': createSamplesApi,
        'update': updateSamplesApi
      },
      form: {
        item_no: undefined,
        warehouse: undefined,
        location: undefined,
        menu: undefined,
        valid_date: undefined,
        invalid_date: undefined,
        note: undefined,
        quantity: undefined
      },
      error: {
        item_no: undefined,
        warehouse: undefined,
        location: undefined,
        menu: undefined,
        valid_date: undefined,
        invalid_date: undefined,
        note: undefined,
        quantity: undefined
      },
      rules: {
        item_no: [
          {
            required: true,
            message: this.$t('samplesItemRequire'),
            trigger: 'blur'
          }
        ],
        menu: [
          {
            required: true,
            message: this.$t('samplesMenuCodeRequire'),
            trigger: 'blur'
          }
        ],
        valid_date: [
          {
            required: true,
            message: this.$t('samplesValidDateRequire'),
            trigger: 'blur'
          }
        ],
        invalid_date: [
          {
            required: true,
            message: this.$t('samplesInvalidDateRequire'),
            trigger: 'blur'
          }
        ]
      }
    };
  },
  methods: {
    initialize() {
      this.getMenuOption()
    },
    async getMenuOption() {
      var resp = await getDropDownMenuListApi();
      if (resp.success) {
        this.menuOptions = resp.data;
      } else {
        this.targaMessageBox(this, resp.msg);
      }
    },
    handleCreate() {
      this.submitForm('create', this.$t('createSuccess', { title: this.$t('samplesItem'), value: this.form.item_no }))
    },
    handleUpdate() {
      this.submitForm('update', this.$t('updateSuccess', { title: this.$t('samplesItem'), value: this.form.item_no }))
    }
  }
};
</script>

<style></style>

